package com.aaa.service.impl.mht;

import com.aaa.entity.MyPage;
import com.aaa.entity.User;
import com.aaa.entity.mht.MhtPlan;
import com.aaa.mapper.mht.MhtPlanMapper;
import com.aaa.service.mht.MhtPlanService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MhtPlanServiceImpl implements MhtPlanService {
    @Autowired
    private MhtPlanMapper mhtPlanMapper;

    /**
     * 添加一个计划
     *
     * @param user
     * @param mhtPlan
     * @return
     */
    @Override
    public Boolean addPlan(User user, MhtPlan mhtPlan) {
        int i = -1;
        Integer id = mhtPlan.getId();
        if(id==null){
            Integer uid = user.getId();
            mhtPlan.setUid(uid);
            mhtPlan.setCreatetime(new Date());
            i = mhtPlanMapper.insert(mhtPlan);
        }else {
            mhtPlan.setUpdatetime(new Date());
            i = mhtPlanMapper.updateByPrimaryKeySelective(mhtPlan);
        }
        return i>=0;
    }

    /**
     * 查询全部的计划信息 并分页
     * @param myPage
     * @param mhtPlan
     * @return
     */
    @Override
    public PageInfo showplan(MyPage myPage, MhtPlan mhtPlan) {
        PageHelper.startPage(myPage.getPage(),myPage.getLimit());
        List<MhtPlan> findplan = mhtPlanMapper.findplan(mhtPlan);
        PageInfo pageInfo = new PageInfo(findplan);
        return pageInfo;
    }

    /**
     * 修改计划的状态码
     * @param mhtPlan
     * @return
     */
    @Override
    public Boolean updPlanstatus(MhtPlan mhtPlan) {
        int i = mhtPlanMapper.updateByPrimaryKeySelective(mhtPlan);
        return i>=0;
    }

    /**
     * 检测计划名称是否存在
     * @param planname
     * @return
     */
    @Override
    public Boolean checkPlanname(String planname) {
        MhtPlan mhtPlan = new MhtPlan();
        mhtPlan.setPlanname(planname);
        MhtPlan mhtPlan1 = mhtPlanMapper.selectOne(mhtPlan);
        if(mhtPlan1!=null){
            return true;
        }else {
            return false;
        }
    }
    /**
     * 检测验证码是否存在
     * @param mhtPlan
     * @return
     */
    @Override
    public Boolean checkcode(MhtPlan mhtPlan) {
        MhtPlan mhtPlan1 = mhtPlanMapper.selectOne(mhtPlan);
        if(mhtPlan1!=null){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 通过验证码获取计划
     * @param mhtPlan
     * @return
     */
    @Override
    public MhtPlan findPlanByCode(MhtPlan mhtPlan) {
        MhtPlan mhtPlan1 = mhtPlanMapper.selectOne(mhtPlan);
        return mhtPlan1;
    }
}
